var app = getApp(), api = getApp().api;

Page({
    data: {
        cat_id: 0,
        keyword: "",
        list: [],
        page: 1,
        no_more: !1,
        loading: !1,
        activeIndex: 0,
        slide: [],
        mch_cat: []
    },
    onLoad: function(t) {
        var a = this;
        getApp().page.onLoad(this, t), t.cat_id && (this.data.cat_id = t.cat_id), app.getLocation(function(o) {
            console.log(o), a.setData({
                lat: o.latitude,
                lng: o.longitude
            }), getApp().request({
                url: getApp().api.default.banner_list,
                data: {
                    category: 2
                },
                success: function(e) {
                    a.setData({
                        slide: e.data
                    })
                }
            }), a.loadShopList();
        }), getApp().request({
            url: getApp().api.default.mch_cat,
            data: {
                cat_id: t.cat_id
            },
            success: function(e) {
                a.setData({
                    mch_cat: e.data
                }), wx.setNavigationBarTitle({
                  title: e.data.name,
                })
            }
        });
        
    },
    loadShopList: function(t) {
        var a = this, c = wx.getStorageSync("city"), y = a.data.type || 1;
        console.log("城市为" + c, y), a.data.no_more ? "function" == typeof t && t() : a.data.loading || (a.setData({
            loading: !0
        }), getApp().request({
            url: getApp().api.mch.shop_list,
            data: {
                keyword: a.data.keyword,
                cat_id: a.data.cat_id,
                page: a.data.page,
                latitude: a.data.lat,
                longitude: a.data.lng,
                cityname: c,
                type: y
            },
            success: function(t) {
                if (0 == t.code) {
                    var c = t.data.cat_list;
                    for (var s in c) {
                        a.data.cat_id == c[s].id ? c[s].checked = !0 : c[s].checked = !1;
                    }
                    console.log(c);
                    if (!t.data.list || !t.data.list.length) return void a.setData({
                        no_more: !0,
                        cat_list: t.data.cat_list
                    });
                    a.data.list || (a.data.list = []), a.data.list = a.data.list.concat(t.data.list), 
                    a.setData({
                        list: a.data.list,
                        page: a.data.page + 1,
                        cat_list: t.data.cat_list
                    });
                }
            },
            complete: function() {
                a.setData({
                    loading: !1
                }), "function" == typeof t && t();
            }
        }));
    },
    tabSwitch: function(t) {
        var a = t.currentTarget.id; 
        this.setData({
            activeIndex: a,
            type: parseInt(a) + 1,
            page: 1,
            list: [],
            no_more: !1,
            loading: !1,
        }), this.loadShopList();
    },
    gotomch: function(t) {
        var a = t.currentTarget.id;
        wx.navigateTo({
          url: '/mch/shop/shop?mch_id=' + a,
        })
    },
    phone: function(t) {
        var a = t.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: a
        });
    },
    Clipboard: function(t) {
        var d = t.currentTarget.dataset.name;
        getApp().core.setClipboardData({
            data: d,
            success (res) {
                console.log(res)
            }
        })
    },
    openmap: function(t) {
        var lat = t.currentTarget.dataset.latitude, lng = t.currentTarget.dataset.longitude, address = t.currentTarget.dataset.address;
        getApp().core.openLocation({
            latitude: parseFloat(lat),
            longitude: parseFloat(lng),
            address: address
        });
    },
    onReady: function() {
        getApp().page.onReady(this);
    },
    onShow: function() {
        var a = this;
        getApp().page.onShow(this);
        getApp().request({
            url: getApp().api.default.nav_list,
            data: {
                cat_id: a.data.cat_id,
                shop_type: 1,
                good_type: 0
            },
            success: function(res) {
               a.setData({
                nav_list: res.data.list,
               })
            }
        })
    },
    onHide: function() {
        getApp().page.onHide(this);
    },
    onUnload: function() {
        getApp().page.onUnload(this);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.loadShopList();
    },
    searchSubmit: function(t) {
        var a = t.detail.value;
        this.setData({
            list: [],
            keyword: a,
            page: 1,
            no_more: !1
        }), this.loadShopList(function() {});
    },
    showCatList: function() {
        this.setData({
            show_cat_list: !0
        });
    },
    hideCatList: function() {
        this.setData({
            show_cat_list: !1
        });
    },
    jumps: function(t) {
        var u = t.currentTarget.dataset.page_url;
        wx.navigateTo({
          url: u,
        })
    },
    toproduct_cat: function() {
        wx.navigateTo({
          url: '/pages/cat/cat?mch_cat_id=' + this.data.cat_id,
        })
    }
});